﻿#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QVector>
#include "Thread/camerathread.h"
#include "Thread/algthread.h"
#include "Thread/saveimgthread.h"
#include "Thread/csvthread.h"
#include "Common/common.h"
#include "Alg/Defect.h"
#include "PlcMV/plcdlg.h"
#include "ProductMV/productdlg.h"
#include "Views/datatabdlg.h"
#include "ui_productdlg.h"
#include "ui_datatabdlg.h"
#include <QTime>
#include "QMouseEvent"


#include "Loging/logingdlg.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    /* Main Tabe Function*/
    void OpenHalconWindows();

    // 定时器
    void systimer();

    // 运行时间
    void SetRunTime();


    // 初始化 相机 检测数据
    void SetStartParam();
    void SetStopParam();
    void RdConfigIni();
    // 保存瞬时数据
    void SaveToIni();
    void RdManufactureIni();
    // 重写虚函数: changeEvent  解决最小化 会导致ui界面 假死
    void changeEvent(QEvent *event);



    void SetAlgHideUi(int index);
    // 获取当前时间
    QString GetTime();


    // Prints out some logging
    void Log(std::string strMsg,int type = 0);

    // 存图  测量
    void ShowSwitchBt(QPushButton* pSwBt,int nState,int nIndex);


    void AlgRule(int nIndex);



    void Msg_Box(int nMsg = 0);


    void SetCombox();


    void SendPlcResult(int result);

    // ui 界面 显示算法 结果数据
    void InitializeAlgData();
    void ShowAlgData(int type);


    /* CCD1 Tabe Function*/


    // 打开图片功能
    void CarouselImage(int index = 0);
    void ShowImage(HalconCpp::HObject img,HalconCpp::HTuple hvHandle);

    void OpenFileImg(int index = 0);


    void ImgUp(int index = 0);

    void ImgDown(int index = 0);

    //相机基本属性
    void getCameraAttribute();
    void setCameraAttribute();



    // 创建文件夹
    void CreateCameraSaveFile();


    void RdProductIni(QString iniFilePath);

    void RdModeId();
    void ShowUI();


    bool deleteDir(const QString &path);

    // 清空数据
    void ClearCameraData();

    // IsPlcConnect()
    void IsPlcConnect();


    // 显示检测值
    void updateAlgInfo();


    // 设置线程 ALG 检测函数
    void setAlgOptions(AlgThread* tAlg,SW_AlgPart ccd);




    /*****************************图像放大缩小 移动功能*************************************/

    HTuple hv_WindowHandle;//窗口句柄
    Hlong windID;//控件句柄
    QPoint lastPoint; //鼠标在控件中的位置
    bool mouseDown; // 鼠标按下标志位
    HObject src1; //读入 初始图像
    float zoom_scale; // 放大倍数
    QPoint windowPoint, firstPoint;//图像移动点和初始点
    int m_dDispImagePartRow0, m_dDispImagePartCol0, m_dDispImagePartRow1, m_dDispImagePartCol1;//显示图像的区域
    int m_dYOffset, m_dXOffset, m_dXO, m_dYO;//图像偏移，与初始位置





    void mousePressEvent(QMouseEvent *event);//鼠标按下事件
    void mouseMoveEvent(QMouseEvent *event);//鼠标按下移动事件
    void mouseReleaseEvent(QMouseEvent *event);//鼠标松开事件
    void wheelEvent(QWheelEvent *event);//鼠标滚轮事件
    void displayImage(HImage srcImg, HTuple hv_Window);//显示图像
    void moveWnd(QPoint point, HImage srcImg, HTuple hWindow);//移动显示区域


public:
    SBaslerCameraControl m_control;
    int m_nCameraNum = 0;      // 系统所需相机数量
    int m_nCurrentCamera = 0;  // 当前相机个数
    QString m_strMsg;
    int m_nResultNum = 0;  // PLC结果




    /* Main Tabe Attribute*/

    // 相机计算数据
    bool m_bStart = false;
    bool m_bSet = false;
    int m_nDelet = 0;  // 删除图片数据
    int m_nNG1 = 0;
    int m_nNG2 = 0;
    int m_nNG3 = 0;
    int m_nNG4 = 0;
    int m_nOK = 0;
    int m_nPlcIndex = 0; // PLC 通信计数
    int m_nRev = 0;  // 速率
    int m_nTime = 0;
    int m_nOldTime = 0;


    int m_nSaveTime = 0;

    int stopTime = 30;

    bool m_nSaveData = false;
    // 速率
    QString strRev = "0";
    QTimer *myTimer;

    // 运行时间
    QString m_sRunTime;

    int m_nSS = 0;
    int m_nMM = 0;
    int m_nHH = 0;
    int m_nDay = 0;

    int m_nCameraResult = 0; // 运算结果总量
    int m_nGod1 = 0;
    int m_nGod2 = 0;
    int m_nGod3 = 0;
    int m_nGod4 = 0;
    int m_nGod5 = 0;
    int m_nGod6 = 0;
    int m_nGod7 = 0;
    int m_nGod8 = 0;


    int m_nBad1 = 0;
    int m_nBad2 = 0;
    int m_nBad3 = 0;
    int m_nBad4 = 0;
    int m_nBad5 = 0;
    int m_nBad6 = 0;
    int m_nBad7 = 0;
    int m_nBad8 = 0;


    int nBF = 0;

    int m_nMainStart = 0;   // 程序启动 0 停止  1 失败

    int m_nMainCurrent = 0; // 当前 跳转的页面序号
    std::vector<QString> m_cameras;


    bool isOpenImg = false; // 是否打开了图像
    QStringList file_list;  // 配置文件列表
    QStringList file_names; // 文件名
    int m_nProductNum = 0;  // 当前产品序号
    int bProSave = false;   // 使瞬时产品得以调用
    /* PLC DLG */
    PlcDlg* m_dPlc;

    bool bConnect;

    // Csv Thread
    CsvThread* m_tCsv;
    CsvData m_csData;
    // Camera Thread
    CameraThread* m_tCamera1;
    CameraThread* m_tCamera2;
    CameraThread* m_tCamera3;
    CameraThread* m_tCamera4;
    CameraThread* m_tCamera5;
    CameraThread* m_tCamera6;
    CameraThread* m_tCamera7;
    CameraThread* m_tCamera8;


    // Alg Thread
    AlgThread* m_tCurrentAlg;


    AlgThread* m_tAlg1;
    AlgThread* m_tAlg2;
    AlgThread* m_tAlg3;
    AlgThread* m_tAlg4;
    AlgThread* m_tAlg5;
    AlgThread* m_tAlg6;
    AlgThread* m_tAlg7;
    AlgThread* m_tAlg8;



    // save img Thread
    SaveImgThread* m_tSaveImg1;
    SaveImgThread* m_tSaveImg2;
    SaveImgThread* m_tSaveImg3;
    SaveImgThread* m_tSaveImg4;
    SaveImgThread* m_tSaveImg5;
    SaveImgThread* m_tSaveImg6;
    SaveImgThread* m_tSaveImg7;
    SaveImgThread* m_tSaveImg8;

    // Main Halcon
    HalconCpp::HTuple m_hvMainHandle1;
    HalconCpp::HTuple m_hvMainHandle2;
    HalconCpp::HTuple m_hvMainHandle3;
    HalconCpp::HTuple m_hvMainHandle4;
    HalconCpp::HTuple m_hvMainHandle5;
    HalconCpp::HTuple m_hvMainHandle6;
    HalconCpp::HTuple m_hvMainHandle7;
    HalconCpp::HTuple m_hvMainHandle8;

    int m_nOpenHalcon = 0;  // 0 未打开  1 打开
    // 存图   测量
    SW_Check m_swCheck;
    SW_SaveImg m_swSaveImg;


    Create_Model m_createModel;
    Read_Model m_readModel;
    /* CCD1~4 Tabe Attribute*/
    HalconCpp::HTuple c1_hvCCDHandle;
    HalconCpp::HTuple c2_hvCCDHandle;
    HalconCpp::HTuple c3_hvCCDHandle;
    HalconCpp::HTuple c4_hvCCDHandle;
    HalconCpp::HTuple c5_hvCCDHandle;
    HalconCpp::HTuple c6_hvCCDHandle;
    HalconCpp::HTuple c7_hvCCDHandle;
    HalconCpp::HTuple c8_hvCCDHandle;

    CameraThread *m_tCurrentCamera;
    // 文件信息
    // 图片切换
    QString m_vFilePath[8];
    QStringList m_vFileList[8];
    int m_vFileIndex[8];

    // 相机基本属性
    Defect m_aDefect;
    HalconCpp::HObject m_hImage;
    HalconCpp::HTuple m_hvHandle;



    int m_nMoto1 = 0;
    int m_nContinuous = 0;


    /* Product Dlg */
    ProductDlg* m_dProduct;


    /* DataTba */
    DataTabDlg* m_dDataTab;


    int m_nDefctType = 0;
    int m_nRectType = 0;


    QString m_strName;

    /* Log Dlg */

    LogingDlg* m_dLog;
public slots:

    void SearchProFiles();

    void on_Main_BtMin_clicked();

    void on_Main_BtQuite_clicked();

    void on_Main_BtStartAndStop_clicked();

    void on_CCD1_OpenFileBt_clicked();

    void on_Main_TabA_currentChanged(int index);

    void on_CCD1_UpBt_clicked();

    void on_CCD1_DownBt_clicked();

    void on_CCD1_StaticBt_clicked();

    void on_CCD_changeBT_clicked();

    void on_Main_BtNGorAND_clicked();

    void on_Main_SBtCCD1_clicked();

    void on_Main_SBtCCD2_clicked();

    void on_Main_SBtCCD3_clicked();

    void on_Main_SBtCCD4_clicked();

    void on_Main_SBtCCD5_clicked();

    void on_Main_SBtCCD6_clicked();

    void on_Main_CBtCCD1_clicked();

    void on_Main_CBtCCD4_clicked();

    void on_Main_CBtCCD2_clicked();

    void on_Main_CBtCCD5_clicked();

    void on_Main_CBtCCD3_clicked();

    void on_Main_CBtCCD6_clicked();

    void on_Main_BtPLCManage_clicked();

    void on_Main_BtPic_clicked();

    // The event handler for the camera list changed event

    void CameraResult(int m_nResult);


    void AlgSwResult();

private slots:

    void on_Main_BtReset_clicked();

    void on_Main_BtProductManage_clicked();

    void on_Main_BtLight_clicked();

    void on_Main_BtDiscA_clicked();

    void on_Main_BtMotorA_clicked();

    void on_Main_BtDiscB_clicked();

    void on_Main_BtLogin_clicked();

    void on_Main_BtLogout_clicked();

    void on_CCD_singleBt_clicked();

    void on_CCD_plateBt_clicked();

    void on_CCD_multiBt_clicked();

    void on_pushButton_9_clicked();

    void on_Main_ComboxProduct_currentIndexChanged(int index);

    void on_changeBt_clicked();

    void on_ClearBt_clicked();

    void on_CCD_placeBt_clicked();

    void on_CSetAlgPlace_clicked();
    void on_CCgeAlgPlace_clicked();

    void on_checkBox_clicked(bool checked);

    void on_Main_BtOpenFile_clicked();

    void on_Main_SBtCCD7_clicked();

    void on_Main_SBtCCD8_clicked();

    void on_Main_CBtCCD7_clicked();

    void on_Main_CBtCCD8_clicked();

    void on_Main_BtLogManage_clicked();

private:
    Ui::MainWindow *ui;

    QWidget* m_tabA;  // 缺陷:方形
    QWidget* m_tabB;  // 尺寸1（长方形 正方形）(全检)
    QWidget* m_tabC;  // 尺寸3(梯形)
    QWidget* m_tabD;  // 异形(正面)
    QWidget* m_tabE;  // 异形(侧面)
    QWidget* m_tabF;  // 尺寸2（长方形 正方形）（高度）
    QWidget* m_tabG;  // 扇形
    QWidget* m_tabH;  // 缺陷:扇形
    QWidget* m_tabI;  // 缺陷:侧面
    QWidget* m_tabJ;  // 缺陷:侧面


};

#endif // MAINWINDOW_H
